/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogConflito.java
 *
 * Created on 09/10/2008, 23:34:28
 */

package sintelo.visao;

import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import sintelo.glc.Regra;
import sintelo.glc.TabelaParse;

/**
 *
 * @author phireis
 */
public class DialogConflito extends javax.swing.JDialog {

    private DefaultListModel lista = new DefaultListModel();
    private TabelaParse.Conflito conflito;
    private int retorno;

    public static int resolve(java.awt.Frame parent, TabelaParse.Conflito conflito){
        DialogConflito dlg = new DialogConflito(parent, conflito);
        dlg.setVisible(true);
        return dlg.getRetorno();
    }

    private class Item{
        int numero;
        String regra;

        public Item(int numero, String regra) {
            this.numero = numero;
            this.regra = regra;
        }

        public int getNumero() {
            return numero;
        }

        public String getRegra() {
            return regra;
        }



        @Override
        public String toString() {
            return regra;
        }


    }

    /** Creates new form DialogConflito */
    public DialogConflito(java.awt.Frame parent, TabelaParse.Conflito conflito) {
        super(parent);
        this.conflito = conflito;
        initComponents();
        lstRegras.setModel(lista);

        lblNaoTerminal.setText("Quando no topo da pilha de expansão estiver o não-terminal " + conflito.getNaoTerminal());
        lblTerminal.setText("e o próximo token reconhecido for " + conflito.getTerminal());

        for(Map.Entry<Integer, Regra> ent : conflito.getValores().entrySet()){
            lista.addElement(new Item(ent.getKey(), ent.getValue().toString()));
        }

    }

    public int getRetorno() {
        return retorno;
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        lblNaoTerminal = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        lblTerminal = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnSeleciona = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstRegras = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Resolver conflito");
        setModal(true);

        jPanel1.setLayout(new java.awt.GridLayout(2, 1));

        lblNaoTerminal.setText("jLabel1");
        jPanel3.add(lblNaoTerminal);

        jPanel1.add(jPanel3);

        lblTerminal.setText("jLabel2");
        jPanel4.add(lblTerminal);

        jPanel1.add(jPanel4);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        btnSeleciona.setText("Seleciona regra");
        btnSeleciona.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelecionaActionPerformed(evt);
            }
        });
        jPanel2.add(btnSeleciona);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        lstRegras.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstRegras);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-555)/2, (screenSize.height-303)/2, 555, 303);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSelecionaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelecionaActionPerformed
        int sel = lstRegras.getSelectedIndex();
        if(sel == -1){
            Mensagens.informa(this, "Selecione uma regra da lista!");
            return;
        }
        Item item = (Item) lista.getElementAt(sel);
        retorno = item.getNumero();
        dispose();
    }//GEN-LAST:event_btnSelecionaActionPerformed

   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSeleciona;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNaoTerminal;
    private javax.swing.JLabel lblTerminal;
    private javax.swing.JList lstRegras;
    // End of variables declaration//GEN-END:variables

}
